
'''
剑指 Offer 22. 链表中倒数第k个节点
'''
from com.siyuan.ListNode import ListNode


class Solution:
    def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
        # 指针1
        p1 = head
        # 指针2
        p2 = head
        # 先移动p2 k次
        for i in range(k):
            p2 = p2.next

        # 同时移动p1\p2
        while p2:
            p1 = p1.next
            p2 = p2.next
        return p1
